<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- 实现图片的回显
             1.通过接口获取用户头像 staffPhoto
             2 将这个值赋值给imageUpload组件中的fileList字段
              1) 父传子
              2) 通过ref
            -->
            <ImageUpload ref="ImageUpload" @getImgData="getImgData" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>

        </el-col>
      </el-row>
    </el-form>

  </div>

</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { getUserInfoApi } from '@/api'
export default {
  props: {
    formData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      EmployeeEnum, // 员工枚举数据
      userInfo: {
        workNumber: '', // 工号
        timeOfEntry: '', // 入职时间
        username: '', // 姓名
        departmentName: '', // 部门
        mobile: '', // 手机
        formOfEmployment: '' // 聘用形式
      },
      imgFileList: []

    }
  },
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  watch: {
  },
  created() {
    // console.log(this.formData)
    // 深克隆
    this.userInfo = JSON.parse(JSON.stringify(this.formData))
    this.getUserInfo()
  },

  methods: {
    // 获取用户头像图片
    async getUserInfo() {
      const { data } = await getUserInfoApi(this.userId)
      // console.log(data)
      this.$refs.ImageUpload.fileList = [{
        url: data.staffPhoto,
        uid: 1
      }]
    },
    // 提交
    saveUser() {
      if (this.imgFileList.length < 1) {
        this.$message.error('请上传头像')
        return
      } else {
        this.userInfo.staffPhoto = this.imgFileList[0].url
        this.$emit('submit', this.userInfo)
      }
    },
    // 获取图片上传组件的文件列表 子传父
    getImgData(val) {
      this.imgFileList = val
      console.log(val, '获取图片上传组件的文件列表')
    }

  }
}
</script>
